import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/common/empty_content_widget.dart';
import 'package:edu/pages/profile/profession_setting/profession_setting_page_logic.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

/*
 * 职业设置页面
 */
class ProfessionSettingPage extends StatefulWidget {
  const ProfessionSettingPage({super.key});

  @override
  State<ProfessionSettingPage> createState() => _ProfessionSettingPageState();
}

class _ProfessionSettingPageState extends State<ProfessionSettingPage> {
  @override
  void initState() {
    super.initState();
    context.read<ProfessionSettingPageLogic>().init();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<ProfessionSettingPageLogic>(
      builder: (context, logic, child) {
        return Scaffold(
          appBar: _buildAppBar(context, logic),
          body: _buildMainBody(context, logic),
        );
      },
    );
  }

  // 构建AppBar
  AppBar _buildAppBar(BuildContext context, ProfessionSettingPageLogic logic) {
    final fontSize =
        (Theme.of(context).appBarTheme.titleTextStyle?.fontSize ?? 20) - 2;
    return AppBar(
      title: const Text(AppStrings.selectProfession),
      actions: [
        TextButton(
          onPressed: logic.currentProfessionName != null ? _okAction : null,
          child: Text(AppStrings.ok, style: TextStyle(fontSize: fontSize)),
        ),
      ],
    );
  }

  // 构建内容
  Widget _buildMainBody(
    BuildContext context,
    ProfessionSettingPageLogic logic,
  ) {
    final professions = logic.professions ?? [];
    if (professions.isEmpty) {
      return EmptyContentWidget(
        isLoading: logic.isLoading,
        isError: logic.isError,
        reloadAction: () {
          logic.loadAllProfessions();
        },
      );
    } else {
      return _buildProfessionList(context, logic);
    }
  }

  // 构建职业列表
  Widget _buildProfessionList(
    BuildContext context,
    ProfessionSettingPageLogic logic,
  ) {
    final professions = logic.professions ?? [];
    final Color dividerColor;
    if (Theme.of(context).brightness == Brightness.light) {
      dividerColor = Colors.grey[200]!;
    } else {
      dividerColor = Colors.grey[800]!;
    }

    return ListView.separated(
      separatorBuilder: (context, index) => Padding(
        padding: const EdgeInsets.only(left: 15),
        child: Divider(color: dividerColor, height: 0.25),
      ),
      itemCount: professions.length,
      itemBuilder: (context, index) {
        final profession = professions[index];
        return ListTile(
          title: Text(profession.title),
          trailing: logic.currentProfessionName == profession.title
              ? Icon(Icons.check, color: Theme.of(context).colorScheme.primary)
              : null,
          onTap: () {
            logic.selectProfession(index);
          },
        );
      },
    );
  }

  void _okAction() {
    final logic = context.read<ProfessionSettingPageLogic>();
    logic.updateProfession().then((value) {
      if (value && mounted) {
        Navigator.of(context).pop();
      }
    });
  }
}
